import time
from yanshi.yundama import indetify
import requests
from selenium import webdriver
from PIL import Image


def get_snap(driver):  # 对目标网页进行截屏。这里截的是全屏
    driver.save_screenshot('full_snap.png')
    page_snap_obj = Image.open('full_snap.png')
    return page_snap_obj


def get_image(driver):  # 对验证码所在位置进行定位，然后截取验证码图片
    img = driver.find_element_by_id('imgvalidate')
    time.sleep(2)
    location = img.location
    print(location)
    size = img.size
    left = location['x']
    top = location['y']
    right = left + size['width']
    bottom = top + size['height']

    page_snap_obj = get_snap(driver)
    image_obj = page_snap_obj.crop((left, top, right, bottom))
    # image_obj.show()
    return image_obj  # 得到的就是验证码




driver = webdriver.Chrome()

driver.get("http://www.hzyotoy.com/index.do?mod=reg")

driver.find_element_by_id("mobile").send_keys("13237017781")
driver.find_element_by_id("sendMobileBtn").click()
# 重点是 识别验证码
image_obj = get_image(driver)
image_obj.save("test.jpg")
src = driver.find_element_by_id("imgvalidate").get_attribute("src")
print(src)
f = open("test.jpg","rb")
response = f.read()
f.close()
ret = indetify(response)
print(ret)
#
driver.find_element_by_id("handvalidate").send_keys(ret)
driver.find_element_by_class_name("layui-layer-btn0").click()

time.sleep(10)
driver.quit()